var fs = require("fs");

function start(request, response) {
  console.log("start函数被调用！");
  response.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
  fs.readFile("./index.html", function(error, file) {
    if (error) {
      console.log(error);
    } else {
      response.end(file);
    }
  });
}

function upload(request, response) {
  console.log("upload函数被调用！");

  var formidable = require("formidable");
  var form = formidable.IncomingForm();
  form.uploadDir = "./images";

  form.parse(request, function(error, fields, files) {
    console.log("表单数据解析完成！");
    // console.log(files);
    fs.renameSync(files.imagefile.path,"./images/test.png");
    response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
    response.write("<img src='/show' >");
    response.end();
  });
}

function show(request, response) {
  console.log("show函数被调用！");
  fs.readFile("./images/test.png",function(error,file){
    if (error) {
        console.log(error);
    } else {
        response.end(file);
    }
  });
}

exports.start = start;
exports.upload = upload;
exports.show = show;
